class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size();
        vector<int> dp(n, 1);
        unordered_map<int, int> hash;
        hash[arr[0]] = 1;

        int maxl = 1;
        for (int i = 1; i < n; ++i)
        {
            if (hash[arr[i] - difference] != 0)
            {
                dp[i] = hash[arr[i] - difference] + 1;
            }
            hash[arr[i]] = dp[i];
            maxl = max(maxl, dp[i]);
        }
        return maxl;
    }
};